<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image src="../../static/images/user-head-login.jpg"></image>
			</view>
			<view class='text-center'>
			    <view>申请获取以下权限</view>
			    <text>获得你的公开信息(昵称，头像、地区等)</text>
			</view>
			<wButton class="wbutton" text="授 权 登 录" open-type="getUserInfo" @click="bindGetUserInfo"></wButton>	
		</view>
	</view>
</template>

<script>
	let _this;
	// import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button	
	import userApi from '@/common/webapi/user.js'
	export default {
		data() {
			return {
			};
		},
		components: {
			// wInput,
			wButton,
		},
		mounted() {
			_this = this;
			this.isLogin();
		},
		methods: {
			isLogin() {},
			startLogin(e) {
				console.log(e)
				//登录
				// if (this.isRotate) {
				// 	//判断是否加载中，避免重复点击请求
				// 	return false;
				// }
				// if (this.phoneData.length == "") {
				// 	uni.showToast({
				// 		icon: 'none',
				// 		position: 'bottom',
				// 		title: '用户名不能为空'
				// 	});
				// 	return;
				// }
				// if (this.passData.length < 5) {
				// 	uni.showToast({
				// 		icon: 'none',
				// 		position: 'bottom',
				// 		title: '密码不正确'
				// 	});
				// 	return;
				// }

				// let param = {
				// 	userName: this.phoneData,
				// 	password: this.passData
				// }
				// userApi.login(param).then(res => {
				// 	console.log("登录成功")
				// }).catch(err => {

				// })
				// _this.isRotate = true
				// setTimeout(function() {
				// 	_this.isRotate = false
				// }, 3000)
			},
			login() {
				// 获取登录用户code
				uni.login({
					provider: 'weixin',
					success: res=>{
						console.log(res);
						if (res.code) {
							let code = res.code;
							uni.setStorageSync('user',this.userInfo);
							// uni.setStorageSync('openId',this.openId)
							//将用户登录code传递到后台置换用户SessionKey、OpenId等信息
							//...写用code置换SessionKey、OpenId的接口
							//置换成功调用登录方法_this.updateUserInfo();
							this.updateUserInfo();
						} else {
							uni.showToast({
								title: '登录失败！',
								duration: 2000
							});
							console.log('登录失败！' + res.errMsg)
						}
					},
				});
			},
			bindGetUserInfo() {
				uni.getUserProfile({
					desc: '登录',
					success: res => {
						console.log(res);
						this.userInfo = res.userInfo
						try {
							this.login();
						} catch (e) {}
					},
					fail: () => {
						console.log('fail');
					}
				})
			},
			//向后台更新信息
			updateUserInfo() {
				uni.showLoading({
					title: '登录中...'
				});
				var params = {
					openId: this.openId,
					nickName: this.userInfo.nickName,
					avatarUrl: this.userInfo.avatarUrl,
					gender: this.userInfo.gender,
					city: this.userInfo.city,
					province: this.userInfo.province,
					country: this.userInfo.country,
					unionId: '',
				}
				//console.log('登录');
				//...后台登录的接口
				uni.hideLoading();
				uni.showToast({
					title: '登录成功!',
					icon: 'success',
					duration: 500
				});
				setTimeout(() => {
					uni.reLaunch({
						url: '/pages/monitor/monitor'
					})
				}, 500)
			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
	
</style>
